package com.android.views.recyclerview;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.views.R;
import com.android.views.databinding.FragmentRecyclerViewBinding;
import com.android.views.databinding.LayoutAdapterHeaderBinding;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.android.views.BaseFragment;
import com.yds.views.YDSTopBar;
import com.yds.views.adapter.YDSRecyclerViewCAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
public class RecyclerViewFragment extends BaseFragment {
    private FragmentRecyclerViewBinding binding;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private MyRecyclerViewCAdatper mAdapter;
    private List<String> mList;
    private int count = 10;

    private Handler mHandler = new Handler();

    public RecyclerViewFragment() {
    }

    public static RecyclerViewFragment newInstance(String param1, String param2) {
        RecyclerViewFragment fragment = new RecyclerViewFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_recycler_view, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.setLifecycleOwner(this);

        binding.mYDSTopBar.setOnTopBarClickListener(new YDSTopBar.OnTopBarClickListener() {
            @Override
            public void onImageViewLeftBackClick() {
                Navigation.findNavController(binding.getRoot()).popBackStack();
            }

            @Override
            public void onTextViewLeftBackClick() {
            }

            @Override
            public void onTextViewRightMoreClick() {
            }

            @Override
            public void onImageViewRightMoreClick() {
            }

            @Override
            public void onImageViewRightMore2Click() {
            }
        });

        binding.mYDSEmptyContentLayout.setOnEmptyViewClickListener(() -> {
            Toast.makeText(getActivity(), "empty click ", Toast.LENGTH_SHORT).show();
        });

        initRecyclerview();
        initData();
    }

    private void initRecyclerview() {
        binding.mSmartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                initData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mAdapter.removeAll();
                initData();
            }
        });

        binding.mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new MyRecyclerViewCAdatper(getActivity());
        LayoutAdapterHeaderBinding headerBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.layout_adapter_header, null, false);
        mAdapter.addHeaderView(headerBinding.getRoot());

        //给childview设置tag 如果不变默认不刷新数据 未防止数据项重复需要重写getItemId
        mAdapter.setHasStableIds(true);

        mAdapter.setOnItemClickListener(new YDSRecyclerViewCAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                Toast.makeText(getActivity(), "onItemClick position = " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {
                Toast.makeText(getActivity(), "onItemLongClick position = " + position, Toast.LENGTH_SHORT).show();
            }
        });
        binding.mRecyclerView.setAdapter(mAdapter);
    }

    private void initData() {
        mList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            mList.add(i + "");
        }

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.addList(mList);

                if (mList.isEmpty()) {
                    binding.mYDSEmptyContentLayout.setEmptyContentViewVisible(true);
                } else {
                    binding.mYDSEmptyContentLayout.setEmptyContentViewVisible(false);
                }

                binding.mSmartRefreshLayout.finishLoadMore();
                binding.mSmartRefreshLayout.finishRefresh();
            }
        }, 500);

    }

}